global with sharing class ctrl_CancelProcess
{
	//For Variables...
	Webservice static string cancelProcessButtonMethod(ID accountID, ID objectId, string objectName, string processName)
    {
    	try
    	{
    		
    		if(objectName == 'Account')
    		{
				//Local Variables...
				List<Account> accountList = new List<Account>([SELECT Id,License_Application_Step__c, License_Application_Type__c, RecordTypeId FROM Account where Id =: objectId ]);
				
				if(accountList.size() > 0)
				{
					List<RecordType> accountRecordType = new List<RecordType>([SELECT Id,SObjectType, IsActive, DeveloperName FROM RecordType where SObjectType =: 'Account' AND IsActive =: true AND DeveloperName =: 'Registered']);
					
					if(accountRecordType.size() > 0)
					{
						//For Cancellation..
		    			if(processName == 'Cancellation')
		    			{
		    				
		    				accountList[0].RecordTypeId = accountRecordType[0].Id;
		    				accountList[0].License_Application_Step__c = 'Archived';
		    				//Always getting the OwnerId of the Archived User... (Comming from the Custom Setting...)	
		    				if(User_List__c.getValues('Archive User') != null)
		    					accountList[0].OwnerId = User_List__c.getValues('Archive User').UserID__c; 			
		    				
		    				
		    				//Now Update the Account...
		    				if(accountList.size() > 0)
		    					update accountList;	
		    					
		    				//Cancel the SLA of the Current Process....
		    				List<SLA__c> slaProcessList = new List<SLA__c>([SELECT ID,CreatedDate,License__c,Process_Type__c,Application_Type__c, isCancelled__c,Tracking__c,Application__c,Amendment__c FROM SLA__c where License__c =: objectId AND Process_Type__c = 'License' AND Application_Type__c =: 'Cancellation' AND isCancelled__c =: false AND isCompleted__c =: false ORDER BY CreatedDate DESC LIMIT 1]);	
	    					
	    					if(slaProcessList.size() > 0)
	    					{
	    						slaProcessList[0].isCancelled__c = true;
	    						//SLA...
	    						if(slaProcessList.size() > 0)
	    							update slaProcessList;
	    					}
	    					
		    			}
		    			else if (processName == 'Renewal')//For Renewal...
		    			{
		    				accountList[0].RecordTypeId = accountRecordType[0].Id;
		    				accountList[0].License_Application_Step__c = 'Archived';	
		    				//Always getting the OwnerId of the Archived User... (Comming from the Custom Setting...)	
		    				if(User_List__c.getValues('Archive User') != null)
		    					accountList[0].OwnerId = User_List__c.getValues('Archive User').UserID__c; 
		    					
		    				//Now Update the Account...
		    				if(accountList.size() > 0)
		    					update accountList;	
		    					
		    				//Cancel the SLA of the Current Process....
		    				List<SLA__c> slaProcessList = new List<SLA__c>([SELECT ID,CreatedDate,License__c,Process_Type__c,Application_Type__c, isCancelled__c,Tracking__c,Application__c,Amendment__c FROM SLA__c where License__c =: objectId AND Process_Type__c = 'License' AND Application_Type__c =: 'Renewal' AND isCancelled__c =: false AND isCompleted__c =: false ORDER BY CreatedDate DESC LIMIT 1]);	
	    					
	    					if(slaProcessList.size() > 0)
	    					{
	    						slaProcessList[0].isCancelled__c = true;
	    						//SLA...
	    						if(slaProcessList.size() > 0)
	    							update slaProcessList;
	    					}	
		    				
		    			}
		    			else if (processName == 'Renewal Amendment - Add Location' || processName == 'Renewal Amendment - Change Location') //For Renewal Amendment...
		    			{
		    				//slaObject.Application_Type__c = 'Renewal Amendment - Add Location';
		    				accountList[0].RecordTypeId = accountRecordType[0].Id;
		    				accountList[0].License_Application_Step__c = 'Archived';	
		    				//Always getting the OwnerId of the Archived User... (Comming from the Custom Setting...)	
		    				if(User_List__c.getValues('Archive User') != null)
		    					accountList[0].OwnerId = User_List__c.getValues('Archive User').UserID__c; 
		    					
		    				//Now Update the Account...
		    				if(accountList.size() > 0)
		    					update accountList;	
	    					
	    					
	    					//For Account Locations...
	    					List<Account_Location__c> accountLocationList = new List<Account_Location__c>([SELECT ID,Account__c,New_Location__c,isRenewed__c FROM Account_Location__c WHERE Account__c =: objectId AND isRenewed__c =: false]);
	    					Set<ID> locationIDs = new Set<ID>(); 
	    					//Now Extracting the Values...
	    					for(Account_Location__c accLocationObject : accountLocationList)
	    					{
	    						if(accLocationObject.New_Location__c != null)
    								locationIDs.add(accLocationObject.New_Location__c);
	    					}
	    					//Now Changing the Location Status....
	    					if(locationIDs.size() > 0)
	    					{
	    						List<Location__c> locationList = new List<Location__c>();
	    						
	    						for(Location__c locationObject : [SELECT Id,Location_Booked_Information_Flag__c FROM Location__c where Id IN: locationIDs])
		    					{
		    						locationObject.Location_Booked_Information_Flag__c = false;
		    						locationList.add(locationObject);
		    					}	
	    					
	    						if(locationList.size() > 0)
	    							update locationList;
	    					}
	    					
	    					//Now Delete the Account Location ....
	    					if(accountLocationList.size() > 0)
	    						delete accountLocationList;
	    					
		    				//Cancel the SLA of the Current Process....
		    				List<SLA__c> slaProcessList = new List<SLA__c>([SELECT ID,CreatedDate,License__c,Process_Type__c,Application_Type__c, isCancelled__c,Tracking__c,Application__c,Amendment__c FROM SLA__c where License__c =: objectId AND Process_Type__c = 'License' AND Application_Type__c =: processName AND isCancelled__c =: false AND isCompleted__c =: false ORDER BY CreatedDate DESC LIMIT 1]);	
	    					
	    					if(slaProcessList.size() > 0)
	    					{
	    						slaProcessList[0].isCancelled__c = true;
	    						//SLA...
	    						if(slaProcessList.size() > 0)
	    							update slaProcessList;
	    					
	    					}
	    					
		    			}	
					}
						
				}
    			
    		}	
    		else if(objectName == 'Amendment')
    		{
    			//Local Variables...
				List<Account> accountList = new List<Account>([SELECT Id,License_Application_Step__c, License_Application_Type__c, RecordTypeId FROM Account where Id =: accountID ]);
				
				//For Amendment...
				List<Amendment__c> amendmentList = new List<Amendment__c>([SELECT Id,Cancelled__c, RecordTypeId FROM Amendment__c where Id =: objectId ]);
				
				if(accountList.size() > 0 && amendmentList.size() > 0)
				{
					List<RecordType> accountRecordType = new List<RecordType>([SELECT Id,SObjectType, IsActive, DeveloperName FROM RecordType where SObjectType =: 'Account' AND IsActive =: true AND DeveloperName =: 'Registered']);
					
					if(accountRecordType.size() > 0)
					{
						//For Amendment...
						amendmentList[0].Cancelled__c = true;
						
						//Now Update the Amendment...
	    				if(amendmentList.size() > 0)
	    					update amendmentList;	
	    					
						//For Accounts...
						accountList[0].RecordTypeId = accountRecordType[0].Id;
	    				accountList[0].License_Application_Step__c = 'Archived';
	    				//Always getting the OwnerId of the Archived User... (Comming from the Custom Setting...)	
	    				if(User_List__c.getValues('Archive User') != null)
	    					accountList[0].OwnerId = User_List__c.getValues('Archive User').UserID__c; 			
	    				
	    				
	    				//Now Update the Account...
	    				if(accountList.size() > 0)
	    					update accountList;	
	    					
	    				//Cancel the SLA of the Current Process....
	    				List<SLA__c> slaProcessList = new List<SLA__c>([SELECT ID,CreatedDate,License__c,Process_Type__c,Application_Type__c, isCancelled__c,Tracking__c,Application__c,Amendment__c FROM SLA__c where License__c =: objectId AND Process_Type__c = 'Amendment' AND Application_Type__c =: processName AND isCancelled__c =: false AND isCompleted__c =: false ORDER BY CreatedDate DESC LIMIT 1]);	
    					
    					if(slaProcessList.size() > 0)
    					{
    						slaProcessList[0].isCancelled__c = true;
    						//SLA...
    						if(slaProcessList.size() > 0)
    							update slaProcessList;
    					}
					}
				}
    		}
    		else if(objectName == 'Tenancy')
    		{
    			if(processName == 'Tenancy Renewal')
    			{
    				//Local Variables...
					List<Tenancy_Contract__c> tenancyList = new List<Tenancy_Contract__c>([SELECT Id, RecordTypeId,Renewal_Start_Date__c, Tenancy_Contract_Renewal_Steps__c FROM Tenancy_Contract__c where Id =: objectId ]);
					
					if(tenancyList.size() > 0)
					{
						List<RecordType> tenancyRecordType = new List<RecordType>([SELECT Id,SObjectType, IsActive, DeveloperName FROM RecordType where SObjectType =: 'Tenancy_Contract__c' AND IsActive =: true AND DeveloperName =: 'New_Tenancy_Contract']);
					
						if(tenancyRecordType.size() > 0)
						{
							tenancyList[0].RecordTypeId = tenancyRecordType[0].Id ;
							tenancyList[0].Renewal_Start_Date__c = null;
                            tenancyList[0].Tenancy_Contract_Renewal_Steps__c = null;
                            
                            //Now update the Tenancy Contract...
                            if(tenancyList.size() > 0)
                            	update tenancyList;
                        	
                        	//Cancel the SLA of the Current Process....
		    				List<SLA__c> slaProcessList = new List<SLA__c>([SELECT ID,CreatedDate,License__c,Process_Type__c,Application_Type__c, isCancelled__c,Tracking__c,Application__c,Amendment__c,Tenancy_Contract__c FROM SLA__c where Tenancy_Contract__c =: objectId AND Process_Type__c = 'License' AND Application_Type__c =: 'Tenancy Renewal' AND isCancelled__c =: false AND isCompleted__c =: false ORDER BY CreatedDate DESC LIMIT 1]);	
	    					
	    					if(slaProcessList.size() > 0)
	    					{
	    						slaProcessList[0].isCancelled__c = true;
	    						//SLA...
	    						if(slaProcessList.size() > 0)
	    							update slaProcessList;
	    					
	    					}
	    					
						}
						
					}
						
    			}
				
    		}
    		
    		return 'Success';
    	}
    	catch(Exception exp)
    	{
    			
    		System.debug('The Error Line : '+ exp.getlineNumber() + ' & The Message : '+ exp.getMessage());
    		return exp.getMessage();
    	}
    	
    } 
	


}